package com.example.demo.Service.Impl;

import com.example.demo.Dao.BookDao;
import com.example.demo.Dao.OrderDao;
import com.example.demo.Dao.ShopUserDao;
import com.example.demo.Entity.Book;
import com.example.demo.Entity.OrderList;
import com.example.demo.Service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private BookDao bookDao;
    @Autowired
    private ShopUserDao shopUserDao;
    @Override
    public int countUserOrderById(String uid) {
        return orderDao.countUserOrderById(uid);
    }

    @Override
    public OrderList selectOrderByOid(long OrderId) {

        return orderDao.selectOrderByOid(OrderId);
    }

    @Override
    public boolean createOrder(OrderList order) {
        if(order.getCustomerAddress().equals("") &&
        shopUserDao.selectUserByUserID(order.getCustomerId()).getAddress().equals("")){
            return false;
        }
        if(order.getCustomerAddress().equals("")){
            order.setCustomerAddress(shopUserDao.selectUserByUserID(order.getCustomerId()).getAddress());
        }
        orderDao.createOrder(order);
        return true;
    }

    @Override
    public ArrayList<OrderList> selectOrderByUid(String uid) {
        return orderDao.selectOrderByUid(uid);
    }

    @Override
    public boolean editOrderRemark(long OrderId, String Remark) {
        try{
            orderDao.editOrderRemark(OrderId, Remark);
        } catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editOrderNumber(long OrderId, String Number) {
        try{
            int number = Integer.parseInt(Number);
            if(number <= 0){
                return false;
            }
            Book book = bookDao.selectBookByISBN(selectOrderByOid(OrderId).getISBN());
            if(number > book.getTotalCount()){
                return false;
            }
            orderDao.editOrderNumber(OrderId, number);
        } catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean editOrderAddress(long OrderId, String Address) {
        orderDao.editOrderAddress(OrderId, Address);
        return true;
    }

    @Override
    public boolean updatePrice(long OrderId, String Number, float Price) {
        try{
            int number = Integer.parseInt(Number);
            orderDao.updatePrice(OrderId, number, Price);
        } catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean cancelOrder(long OrderId) {
        orderDao.cancelOrder(OrderId);
        return true;
    }

    @Override
    public boolean confirmOrder(long OrderId) {
        orderDao.confirmOrder(OrderId);
        return true;
    }

    @Override
    public void payOrder(long OrderId) {
        orderDao.payOrder(OrderId);
    }

    @Override
    public int noPayOrder(String UserId) {
        return orderDao.noPayOrder(UserId);
    }

    @Override
    public OrderList selectNewestOrder() {
        return orderDao.selectNewestOrder();
    }

}
